package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.elec.mac.entity.*;
import org.elec.mac.page.PageList;
import org.elec.mac.page.PageProperty;
import org.elec.mac.service.SAdvertService;
import org.elec.mac.service.SAttachedService;
import org.elec.mac.service.SMaterialService;
import org.elec.mac.service.SProductService;
import org.elec.mac.utils.JSONUtil;
import org.elec.mac.utils.UpFileUtils;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 广告
 * Created by Bean on 2018/3/27.
 */
@Slf4j
@Controller
public class AdvertController extends BaseController {

    @Value("${image.path}")
    private String imagepath;

    @Autowired
    private SAdvertService sAdvertService;

    @RequestMapping(value = "/advert_list.do")
    public String deviceListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                    @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                    @RequestParam(value = "limit", required = false) String limit,
                                    @RequestParam(value = "page", required = false) String page) throws IOException {
        if("page".equals(action)) {
            PageProperty pp = new PageProperty();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                this.setPageInfo(request.getCookies(), pp, limit, page);
                PageList<SAdvert> lintPageList = sAdvertService.getPageList(pp);
                map.put("success", true);
                map.put("records", lintPageList.getRecords());
                map.put("recordsTotal", lintPageList.getTotalRecords());
                map.put("recordsFiltered", lintPageList.getTotalRecords());
            } catch(Exception ex) {
                log.error("图片列表获取错误", ex);
                map.put("success", false);
                map.put("msg", "图片列表获取错误");
            }
            response.getWriter().write(JSONUtil.map2json(map));
            return null;
        }
        return "/advert/advert_list";
    }

    @RequestMapping(value = "/advert_add.do")
    public void deviceAddHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                 @RequestParam(value = "action", required = false, defaultValue = "") String action) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");

        try {
            String name = request.getParameter("addname");
            String type = request.getParameter("addtype");
            String path = UpFileUtils.upLoadImageFile(request, imagepath,"addpath");
            SAdvert advert = new SAdvert();
            advert.setName(name);
            advert.setType(Utils.parseInt(type,0));
            advert.setPath(path);
            advert.setState(1);
            sAdvertService.insert(advert);
            log.info("广告图新增");

        } catch(Exception ex) {
            log.error("广告图新增错误", ex);
            result.setSuccess(false);
            result.setMsg("广告图新增错误");
        }

        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }

    @RequestMapping(value = "/advert_edit.do")
    public void deviceEditHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                  @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                  @RequestParam(value = "id", required = false, defaultValue = "") String id) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");

        if("doEdit".equals(action)) {
            try {
                String state = request.getParameter("state");
                if(result.isSuccess()) {
                    SAdvert sAdvert = new SAdvert();
                    sAdvert.setId(Utils.parseInt(id,0));
                    sAdvert.setState(Utils.parseInt(state,0));
                    sAdvertService.update(sAdvert);
                    log.info("广告图状态修改");
                }
            } catch(Exception ex) {
                log.error("广告图状态修改错误", ex);
                result.setSuccess(false);
                result.setMsg("广告状态修改错误");
            }
        }

        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }

}
